/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objectExtraction;

import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_core.cvInRangeS;
import static com.googlecode.javacv.cpp.opencv_core.cvScalar;



import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

import com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

/**
 *
 * @author Shashindra
 */
public class ColourDetector {

    static CvScalar rgba_min = cvScalar(0, 0, 130, 0);// RED wide dabur birko
    static CvScalar rgba_max = cvScalar(80, 80, 255, 0);

    private CvScalar hsv_min1 = cvScalar(0, 45, 0, 0);
    private CvScalar hsv_max1 = cvScalar(7, 255, 255, 0);


    private CvScalar hsv_min2 = cvScalar(173, 45, 0, 0);
    private CvScalar hsv_max2 = cvScalar(180, 255, 255, 0);



    //get tHreshold Image 1 is called from other classes
    public IplImage getThresholdImage1(IplImage orgImg) {
        IplImage bacImage=cvCreateImage(cvGetSize(orgImg), 8, 3);

        //System.out.println("came here");
        IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);

        cvInRangeS(orgImg, rgba_min, rgba_max, imgThreshold);// red

         cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN,5);
        IplConvKernel rect_12 = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_ELLIPSE, null);
        IplConvKernel rect_6 = cvCreateStructuringElementEx(5, 5, 3, 3, CV_SHAPE_ELLIPSE, null);

        cvMorphologyEx(imgThreshold, imgThreshold, bacImage, rect_6, CV_MOP_CLOSE, 1);
        cvMorphologyEx(imgThreshold, imgThreshold, bacImage, rect_12, CV_MOP_OPEN, 1);

        //cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 15);
        //cvSaveImage(++ii + "GreenThreshold.jpg", imgThreshold);
        // System.out.println("hghsgsgsg");
        return imgThreshold;
    }

     public IplImage getThresholdImage2(IplImage orgImg) {

        //System.out.println("came here");
         IplImage imgHSV = cvCreateImage(cvGetSize(orgImg), 8, 3);
         IplImage imgThreshold1 = cvCreateImage(cvGetSize(orgImg), 8, 1);
         IplImage imgThreshold2 = cvCreateImage(cvGetSize(orgImg), 8, 1);

        cvCvtColor(orgImg, imgHSV, CV_BGR2HSV);
        
        cvInRangeS(imgHSV, hsv_min1, hsv_max1, imgThreshold1);

        cvInRangeS(imgHSV, hsv_min2, hsv_max2, imgThreshold2);

        cvOr(imgThreshold1,imgThreshold2,imgThreshold1,null);
        //cvInRangeS(orgImg, rgba_min, rgba_max, imgThreshold);// red

        //cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 15);
        //cvSaveImage(++ii + "GreenThreshold.jpg", imgThreshold);
        // System.out.println("hghsgsgsg");
        return imgThreshold1;
    }
}
